package xzit.gcxy.xxhzx.zhh.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import xzit.gcxy.xxhzx.zhh.common.Pagination;
import xzit.gcxy.xxhzx.zhh.dao.BaseDao;
import xzit.gcxy.xxhzx.zhh.dao.SystemDao;
import xzit.gcxy.xxhzx.zhh.entity.ScienceWorkDetailVO;
import xzit.gcxy.xxhzx.zhh.entity.TeacherInfoVO;
import xzit.gcxy.xxhzx.zhh.entity.TeacherSWTotalVO;
import xzit.gcxy.xxhzx.zhh.service.ScienceWorkService;
import xzit.gcxy.xxhzx.zhh.service.TeacherService;
import xzit.gcxy.xxhzx.zhh.util.InstituteUtil;

@Service("scienceWorkService")
public class ScienceWorkServiceImpl extends SystemServiceImpl implements ScienceWorkService {

	@Autowired
	private SystemDao systemDao;

	public Pagination<ScienceWorkDetailVO> findPageData(DetachedCriteria condition, ScienceWorkDetailVO ce,
			String gonghao, int instituteId, int page, int rows) {
		Pagination<ScienceWorkDetailVO> pagination = new Pagination<ScienceWorkDetailVO>(page, rows);
		condition.createAlias("teacherInfoVO", "t");
		condition.createAlias("scienceworktype", "s");
		condition.add(Restrictions.eq("instituteid", InstituteUtil.getCurrentInstituteId()));
		if (gonghao != null && !"".equals(gonghao)) {
			condition.add(Restrictions.like("t.gonghao", "%" + gonghao + "%"));
		}
		condition.addOrder(Order.asc("t.gonghao"));
		condition.addOrder(Order.asc("s.id"));
		int total = this.systemDao.getRowCountByDetachedCriteria(condition);
		pagination.setTotalCount(total);
		condition.setProjection(null);
		if (total != 0) {
			List<ScienceWorkDetailVO> datas = systemDao.findByDetachedCriteria(condition, page, rows);
			pagination.setDatas(datas);
		}
		return pagination;
	}

	public Pagination<TeacherSWTotalVO> findPageData(int page, int rows) {
		Pagination<TeacherSWTotalVO> pagination = new Pagination<TeacherSWTotalVO>(page, rows);
		List<TeacherSWTotalVO> datas = systemDao.getTotalScore4Teacher();
		
		List<TeacherSWTotalVO> dataret = new ArrayList<TeacherSWTotalVO>();
		int start = (page - 1) * rows;
		while(dataret.size()<rows && start < datas.size()){
			dataret.add(datas.get(start++));
		}
		
		pagination.setTotalCount(datas.size());
		pagination.setDatas(dataret);
		return pagination;
	}

	public List<TeacherSWTotalVO> queryTeacherSWTotal() {
		List<TeacherSWTotalVO> datas = systemDao.getTotalScore4Teacher();
		return datas;
	}

	public List<ScienceWorkDetailVO> queryScienceWorkDetailVOs() {
		DetachedCriteria condition = DetachedCriteria.forClass(ScienceWorkDetailVO.class);
		condition.createAlias("teacherInfoVO", "t");
		condition.createAlias("scienceworktype", "s");
		condition.add(Restrictions.eq("s.instituteid", InstituteUtil.getCurrentInstituteId()));
		
		condition.addOrder(Order.asc("t.gonghao"));
		condition.addOrder(Order.asc("s.id"));
		
		return systemDao.findByDetachedCriteriaNoPage(condition);
	}

}
